﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Examine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Examine
{
    public class QcProcessStandardServices : IQcProcessStandardServices
    {
        public QcProcessStandardServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        //新增
        public async Task<bool> AddAsync(QcProcessStandard model, List<QcProcessStandardItem> list)
        {
            var result = await Db.InsertIdentityTranAsync(model, list);
            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcProcessStandard where Id in @Ids;delete QcProcessStandardItem where ProcessStandardNumber in (select Number from QcProcessStandard where Id in @Ids);";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
        //删除
        public async Task<bool> DeleteAsync(int Id)
        {

            string sql = "delete QcProcessStandard where Id = @Id;delete QcProcessStandardItem where ProcessStandardNumber = (select  Number from QcProcessStandard where Id = @Id);";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }
        //查询所有数据
        public async Task<IEnumerable<QcProcessStandard>> GetAllAsync()
        {
            string sql = "select * from QcProcessStandard";
            var list = await Db.QueryAllAsync<QcProcessStandard>(sql);
            return list;
        }
        //分页查询
        public async Task<IEnumerable<QcProcessStandard>> GetAsync(int PageIndex, int PageSize, string Number, string Name, string Method)
        {
            string sql = "select * from QcProcessStandard where 1=1 ";

            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }


            if (!string.IsNullOrEmpty(Method))
            {
                sql += " and Method  = @Method ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<QcProcessStandard>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", Name = $"%{Name}%", Method = $"%{Method}%" });
            return list;
        }
        //获取数据总条数
        public async Task<int> GetCountAsync(string Number, string Name, string Method)
        {
            string sql = "select count(*) from QcProcessStandard where 1=1 ";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number ";
            }

            if (!string.IsNullOrEmpty(Name))
            {
                sql += " and Name like @Name ";
            }
            if (!string.IsNullOrEmpty(Method))
            {
                sql += " and Method  = @Method ";
            }


            var count = await Db.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", Name = $"%{Name}%", Method = $"%{Method}%" });
            return count;
        }

        public async Task<IEnumerable<QcProcessStandardItem>> GetQcProcessStandardItemByNumber(string Number)
        {
            var list = await Db.QueryAsync<QcProcessStandardItem>("select  * from QcProcessStandardItem where ProcessStandardNumber = @Number", new { Number });
            return list;
        }

        //修改
        public async Task<bool> UpdateAsync(QcProcessStandard model, List<QcProcessStandardItem> list)
        {
            var result = await Db.UpdateIdentityTranAsync(model, list, "Id", "ProcessStandardNumber", "Number");
            return result;
        }





    }
}
